package com.micro.school.system.controller.base;

import com.micro.school.common.core.context.SecurityContextHolder;
import com.micro.school.common.core.utils.file.OssUtil;
import com.micro.school.common.core.web.controller.BaseController;
import com.micro.school.common.core.web.domain.AjaxResult;
import com.micro.school.common.core.web.page.TableDataInfo;
import com.micro.school.system.api.factory.RemoteFileFallbackFactory;
import com.micro.school.system.domain.BannerResult;
import com.micro.school.system.service.BannerResultService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * (BannerResult)表控制层
 *
 * @author makejava
 * @since 2025-08-08 19:07:23
 */
@RestController
@RequestMapping("/bannerResult")
public class BannerResultController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(BannerResultController.class);

    /**
     * 服务对象
     */
    @Resource
    private BannerResultService bannerResultService;

    /**
     * 分页查询
     *
     * @param bannerResult 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public TableDataInfo listByPage(BannerResult bannerResult) {
        startPage();
        List<BannerResult> bannerResultList = this.bannerResultService.queryByPage(bannerResult);
        return getDataTable(bannerResultList);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult getById(@PathVariable("id") Integer id) {
        return success(this.bannerResultService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param bannerResult 实体
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult save(@RequestBody BannerResult bannerResult) {
        bannerResult.setTenantId(Integer.valueOf(SecurityContextHolder.getUserTenantId()));
        return success(this.bannerResultService.insert(bannerResult));
    }

    /**
     * 编辑数据
     *
     * @param bannerResult 实体
     * @return 编辑结果
     */
    @PutMapping
    public AjaxResult modify(BannerResult bannerResult) {
        return success(this.bannerResultService.update(bannerResult));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public AjaxResult deleteById(Integer id) {
        return success(this.bannerResultService.deleteById(id));
    }


    /**
     * 批量上传Banner图片到OSS
     *
     * @param file 图片文件数组
     * @return 上传结果
     */
    @PostMapping("/upload")
    public AjaxResult uploadBannerImages(@RequestParam("file") MultipartFile file) {
        if (file == null ) {
            return error("未选择文件");
        }
        try {
            // 生成OSS对象名称
            String objectName = OssUtil.generateOSSObjectName(file);

            // 上传到OSS
            String url = OssUtil.uploadToOSS(file.getInputStream(), objectName);


            // 保存到数据库
            BannerResult bannerResult = new BannerResult();
            bannerResult.setTenantId(Integer.valueOf(SecurityContextHolder.getUserTenantId()));
            bannerResult.setIsDel(0);
            List<BannerResult> bannerResults = bannerResultService.queryByPage(bannerResult);
            if (bannerResults.size() == 5){
                return AjaxResult.error("文件上传已达最大值");
            }
            bannerResult.setCreateTime(new Date());
            bannerResult.setBannerPath(url);
            bannerResult.setUpdateTime(new Date());
            bannerResultService.insert(bannerResult);
            return AjaxResult.success("文件上传成功！",url);
        } catch (IOException e) {
            log.error("文件转换为输入流失败: {}", file.getOriginalFilename(), e);
        } catch (Exception e) {
            log.error("上传文件到OSS失败: {}", file.getOriginalFilename(), e);
        }

        return AjaxResult.success("文件上传成功！");
    }

}

